//
//  BadgeNumberLabel.m
//  TQMall
//
//  Created by Johnny on 3/24/14.
//  Copyright (c) 2014 Johnny. All rights reserved.
//

#import "BadgeNumberLabel.h"

@implementation BadgeNumberLabel

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
//        self.backgroundColor = [UIColor redColor];
        self.layer.borderWidth = 0.5;
        self.layer.borderColor = [UIColor redColor].CGColor;
        self.layer.cornerRadius = self.bounds.size.width / 2.0;
        self.font = [UIFont systemFontOfSize:10];
        self.textColor = [UIColor blueColor];
        self.clipsToBounds = YES;
    }
    
    return self;
}

- (void)setBadgeNumber:(NSInteger)badgeNumber
{
    _badgeNumber = badgeNumber;
    
    NSString *badgeString;
    if (badgeNumber > 99) {
        badgeString = @" 99+ ";
    }
    else if (badgeNumber > 0) {
        badgeString = [NSString stringWithFormat:@" %ld ", (long)badgeNumber];
    }
    else {
        badgeString = @"";
    }
    
    if (badgeString.length == 0) {
        self.hidden = YES;
    }
    else {
        self.hidden = NO;
        self.text = badgeString;
        [self sizeToFit];
        
        CGFloat width = 0.0;
        if (self.bounds.size.width > self.bounds.size.height) {
            width = self.bounds.size.width;
        }
        else {
            width = self.bounds.size.height;
        }
        
        if (width > 24) {
            width = 24;
        }
        self.frame = CGRectMake(0, 0, width, width);
        
        self.layer.cornerRadius = self.bounds.size.width / 2.0;
    }
}

@end
